﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;
using Charities.ModelViews;
using System.IO;

namespace Charities
{
    public partial class CustomSearchPrintForm : Form
    {
        public int uid { get; set; }
        public string title { get; set; }
        Form mdi = null;
        List<Worthy> worthies = null;
        public bool is_loaded = false;
        ModelContainer ctx = new ModelContainer();
        public CustomSearchPrintForm(int userId, Form fo, List<Worthy> _worthies,string Title)
        {
            uid = userId;
            MdiParent = fo;
            mdi = fo;
            worthies = _worthies;
            title = Title;
            InitializeComponent();
        }

        private void CustomSearchPrintForm_Load(object sender, EventArgs e)
        {
            List<ModelViews.WorthiesView> wView = new List<ModelViews.WorthiesView>();
            foreach (var w in worthies)
            {
                Person p = ctx.People.First(c => c.Id == w.PersonId);
                ModelViews.WorthiesView wv = new ModelViews.WorthiesView();
                wv.Id = (int)p.WorthyId;
                wv.insertDate = p.InsertDate;
                wv.OreginalAddress = w.OriginalAddress;
                wv.RelativesCount = w.People.Count();
                wv.CurrentAddress = w.CurrentAddress;
                wv.DisplayName = p.FirstName + " " + p.FatherName + " " + p.LastName;
                wv.WorthyTypeName = w.WorthyType.Name;
                if(w.InsertDate!=null)
                wv.FromDate = w.InsertDate.Value;
                wv.PhoneNumber = w.Phone;
                wView.Add(wv);
            }

            WorthiesViewBindingSource.DataSource = wView;
            GrantViewDetail gview = new GrantViewDetail();
            gview.Discreption = title;
            
            GrantViewDetailBindingSource.DataSource = gview;
            if (ctx.CharityInfos.Count() > 0)
            {
                CharityInfoBindingSource.DataSource = ctx.CharityInfos.First();
            }
            else
            {
                MessageBox.Show("يجب تنصيب الاعدادات الرئيسية حتى تعمل التقارير بالشكل الامثل ", "تنويه", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            this.reportViewer1.RefreshReport();

        }
        public byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        }

        private void WorthiesNamesReport_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;
            // remove this form from windows list
            ((Forms.FormMDI)mdi).removeFormFromWindowsList(this);
        }

        private void WorthiesNamesReport_Activated(object sender, EventArgs e)
        {
            // activate this form and add it windows list as activate one
            ((Forms.FormMDI)this.MdiParent).activateWindow(this);
        }
    }
}
